package com.software.levelupserver.controller;

import com.software.levelupserver.entity.Task;
import com.software.levelupserver.service.TaskService;
import com.software.levelupserver.utils.DateUtil;
import com.software.levelupserver.utils.ExperienceUtil;
import com.software.levelupserver.utils.GradeUtil;
import com.software.levelupserver.utils.ResultVOUtil;
import com.software.levelupserver.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/task")
public class TaskController {
    @Resource
    private TaskService taskService;

    /**
     * 新增任务（包含各种任务类型）
     * @param task
     * @return
     */
    @PostMapping("/insertTask")
    public Result insertTask(@RequestBody Task task){
        double experience = ExperienceUtil.getExperience(task);
        task.setExperience(experience);

        int result = taskService.insertTask(task);

        Result resp = null;
        if (result > 0)

            resp = ResultVOUtil.success("新增任务成功");
        else
            resp = ResultVOUtil.error("新增任务失败");
        return resp;
    }

    /**
     * 任务更新（包含各种任务）
     * @param task
     * @return
     */
    @PostMapping("/updateTask")
    public Result updateTask(@RequestBody Task task){
        double experience = ExperienceUtil.getExperience(task);
        task.setExperience(experience);

        boolean result = taskService.updateTask(task);

        Result resp = null;
        if (result){
            resp = ResultVOUtil.success("更新任务成功");
            //调用接口实现

        }else {
            resp = ResultVOUtil.error("更新任务失败");
        }
        return resp;
    }

    /**
     * 任务删除
     * @param task
     * @return
     */
    @PostMapping("/deleteTask")
    public Result deleteTask(@RequestBody Task task){
        boolean result = taskService.deleteTask(task);

        Result resp = null;
        if (result){
            resp = ResultVOUtil.success("删除成功");
        }else {
            resp = ResultVOUtil.error("删除失败");
        }
        return resp;
    }

    /**
     * 获取当前用户的所有待办
     * @return
     */
    @GetMapping("/getAllToDoTasks")
    public Result getAllToDoTasksByUserId(){
        List<Task> allToDoTasks = taskService.getAllTodoTasksByUserId();

        return ResultVOUtil.success(allToDoTasks);
    }

    /**
     * 查询指定日期的所有任务
     * @param date
     * @return
     */
    @GetMapping("/getAllTasksByDate")
    public Result getAllTasksByDate(String date){
        List<Task> allTodayTasks = taskService.getAllTasksByDate(date);

        return ResultVOUtil.success(allTodayTasks);
    }

    /**
     * 获取指定日期和类型的任务
     * @param date
     * @param taskType
     * @return
     */
    @GetMapping("/getTodayTasksByType")
    public Result getTodayTasksByType(String date, int taskType){
        List<Task> todayTasksByType = taskService.getTodayTasksByType(date, taskType);

        return ResultVOUtil.success(todayTasksByType);
    }

    /**
     * 分页查询开始时间在今日的任务
     * @param date
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getTodayTasks")
    public Result getTodayTask(String date, int pageNum, int pageSize){
        List<Task> tasks = taskService.getTodayTasks(date, pageNum, pageSize);

        return ResultVOUtil.success(tasks);
    }

    /**
     * 获取无开始时间的任务
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getNoTimeTasks")
    public Result getNoTimeTask(int pageNum, int pageSize){
        List<Task> tasks = taskService.getNoTimeTask(pageNum, pageSize);

        return ResultVOUtil.success(tasks);
    }

    /**
     * 根据任务类型分页查询任务
     * @param date
     * @param taskType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getTasksByTaskType")
    public Result getTasksByTaskType(String date, int taskType, int pageNum, int pageSize){
        List<Task> tasks = taskService.getTasksByTaskType(date, taskType, pageNum, pageSize);

        return ResultVOUtil.success(tasks);
    }

    /**
     * 根据taskId查询到指定的task
     * @param taskId
     * @return
     */
    @GetMapping("/getTaskByTaskId")
    public Result getTaskByTaskId(int taskId){
        Task task = taskService.getTaskByTaskId(taskId);

        return ResultVOUtil.success(task);
    }

    /**
     * 获取指定类型的所有任务
     * @param status
     * @return
     */
    @GetMapping("/getAllTasksByStatus")
    public Result getAllTasksByStatus(int status){
        List<Task> tasks = taskService.getAllTasksByStatus(status);

        return ResultVOUtil.success(tasks);
    }

    /**
     * 通过指定的日期和优先级获取任务列表
     * @param date
     * @param priority
     * @return
     */
    @GetMapping("/getTasksByPriority")
    public Result getTasksByPriority(String date, int priority){
        List<Task> tasks = taskService.getTasksByPriority(date, priority);

        if (tasks != null){
            return ResultVOUtil.success(tasks);
        }else {
            return ResultVOUtil.error("获取数据失败");
        }


    }

    @GetMapping("/getAllTasks")
    public Result getAllTasks() {
        return ResultVOUtil.success(taskService.selectAllTasks());
    }

    @GetMapping("/countTodayTasks")
    public Result countTodayTasks(){
        int todayTasks = taskService.countTasks();

        return ResultVOUtil.success(todayTasks);
    }

    /**
     * 获取数据库中的所有任务数量
     * @return
     */
    @GetMapping("/countAllTask")
    public Result countAllTask(){
        int allTaskNum = taskService.countAllTask();

        return ResultVOUtil.success(allTaskNum);
    }

    @GetMapping("/getWeekData")
    public Result getWeekData(){
        return ResultVOUtil.success(taskService.selectCountByDate());
    }

    @GetMapping("/getTasksByMonthAndStatus")
    public Result getTasksByMonthAndStatus(String month){
        List<Task> tasks = taskService.getTasksByMonthAndStatus(month);
        if (tasks != null){
            return ResultVOUtil.success(tasks);
        }else {
            return ResultVOUtil.error("获取数据失败");
        }

    }
}
